package com.system.ad.index.cretiveunit;

import com.system.ad.index.IndexAware;
import com.system.ad.index.adunit.AdUnitVo;
import com.system.ad.index.creative.CreativeVo;
import com.system.ad.utlis.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * 创意与推广单元关联索引对象定义与服务
 */
@Slf4j
@Component
public class CreativeUnitIndex implements IndexAware<String, CreativeUnitVo> {

    // 根据广告id获取创意对象
    private static Map<String, CreativeUnitVo> creativeUnitMap = new ConcurrentHashMap();

    // 根据广告id获推广单元id
    private static Map<Long, Set<Long>> adIdUnitIdMap = new ConcurrentHashMap<>();

    // 根据推广单元id获取创意广告id
    private static Map<Long, Set<Long>> unitAdIdMap = new ConcurrentHashMap<>();


    public List<Long> selectAds(List<AdUnitVo> adUnitVos) {
        if (CollectionUtils.isNotEmpty(adUnitVos)) {
            return Collections.emptyList();
        }
        ArrayList<Long> result = new ArrayList<>();
        for (AdUnitVo adUnitVo : adUnitVos) {
            Set<Long> adIds = unitAdIdMap.get(adUnitVo.getUnitId());
            if (CollectionUtils.isNotEmpty(adIds)) {
                result.addAll(adIds);
            }

        }
        return result;
    }


    @Override
    public CreativeUnitVo get(String key) {
        return creativeUnitMap.get(key);
    }


    @Override
    public void add(String key, CreativeUnitVo value) {
        log.info("creativeUnitIndex ,before add:{}", creativeUnitMap);
        creativeUnitMap.put(key, value);

        Set<Long> unitSet = adIdUnitIdMap.get(value.getAdId());
        if (CollectionUtils.isEmpty(unitSet)) {
            unitSet = new ConcurrentSkipListSet<>();
            adIdUnitIdMap.put(value.getAdId(), unitSet);
        }
        unitSet.add(value.getUnitId());


        Set<Long> adSet = unitAdIdMap.get(value.getUnitId());
        if (CollectionUtils.isEmpty(adSet)) {
            adSet = new ConcurrentSkipListSet<>();
            unitAdIdMap.put(value.getUnitId(), adSet);
        }
        adSet.add(value.getAdId());

        log.info("creativeUnitIndex ,after add:{}", creativeUnitMap);

    }

    @Override
    public void update(String key, CreativeUnitVo value) {
        log.error("Creative index cat not support update");
    }

    @Override
    public void delete(String key, CreativeUnitVo value) {
        log.info("creativeUnitIndex ,before delete:{}", creativeUnitMap);

        creativeUnitMap.remove(key);

        Set<Long> unitSte = adIdUnitIdMap.get(value.getAdId());
        if (CollectionUtils.isNotEmpty(unitSte)) {
            unitSte.remove(value.getUnitId());
        }

        Set<Long> adSte = unitAdIdMap.get(value.getUnitId());
        if (CollectionUtils.isNotEmpty(adSte)) {
            adSte.remove(value.getAdId());
        }

        log.info("creativeUnitIndex ,after delete:{}", creativeUnitMap);
    }
}
